import java.util.Arrays;

public class MyarrayList implements IList {

    public int[] elem;
    public int usedSize;
    public static final int capacity = 5;

    public MyarrayList() {
        elem = new int[capacity];
    }


    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public void add(int data) {
        if(isFullAdd()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        elem[usedSize] = data;
        usedSize++;

    }
    public boolean isFullAdd() {
        return usedSize == capacity;
    }

    @Override
    public void add(int pos, int data) {
        if(pos < 0 || pos > usedSize) {
            throw new PosException("Pos位置为："+ pos);
        }
        if(isFullAdd()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
        for (int i = usedSize - 1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind ) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind ) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos < 0 || pos >= usedSize ) {
            throw new PosException("Pos位置为："+ pos);
        }
        if(usedSize == 0) {
            throw new EmptyException("顺序表为空");

        }

        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos < 0 || pos >= usedSize ) {
            throw new PosException("Pos位置为："+ pos);
        }
        if(usedSize == 0) {
            throw new EmptyException("顺序表为空");
        }
        elem[pos] = value;

    }

    @Override
    public void remove(int toRemove) {
        if(usedSize == 0) {
            throw new EmptyException("顺序表为空");
        }
        int index = indexOf(toRemove);
        for (int i = index ; i < usedSize - 1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;


    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        usedSize = 0;

    }
}
